package sqltext

import "fmt"

func BuildSubQuerySql(colIndex string, columns string, table string, where string, grid bool, ds string) string {
	// limit m, n     -- m, n offset，rows
	var querySql string
	qoute := "`"
	if grid {
		if where == "" {
			querySql = fmt.Sprintf("select %s, CRC32(concat_ws('#', %s)) as checksum from %s%s%s%s%s ", colIndex, columns, qoute, ds, qoute, ".", table)
		} else {
			querySql = fmt.Sprintf("select %s, CRC32(concat_ws('#', %s)) as checksum from %s%s%s%s%s%s ", colIndex, columns, qoute, ds, qoute, ".", table, where)
		}
	} else {
		if where == "" {
			querySql = fmt.Sprintf("select %s, CRC32(concat_ws('#', %s)) as checksum from %s ", colIndex, columns, table)
		} else {
			querySql = fmt.Sprintf("select %s, CRC32(concat_ws('#', %s)) as checksum from %s%s ", colIndex, columns, table, where)
		}
	}
	return querySql
}
